package com.springframework.cloud.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.loadbalancer.RandomRule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class CommoditiesController {

    @Value("${server.port}")
    private int port;

    public String fallback(){
        return "当前服务触发熔断";
    }

    @GetMapping("commodities/showMsg")
    @HystrixCommand(defaultFallback = "fallback")
    public String  showMsg(){
       log.info("进入商品信息[{}]",port);
       return String.format("进入商品信息,当前服务的端口:%d",port);
    }

    @GetMapping("commodities/findById")
    @HystrixCommand(defaultFallback = "fallback")
    public Map<String,Object> findById(String commodityId){
        Map<String,Object> map = new HashMap<>();
        log.info("商品服务，接收到商品id为：【{}】",commodityId);
        map.put("status",true);
        map.put("msg","根据商品id查询成功");
        map.put("commodityId",commodityId);
        return map;
    }

    @PostMapping("commodities/saveCommodity")
    public Map<String,Object> saveCommodity(String commodityName){
        Map<String,Object> map = new HashMap<>();
        log.info("保存商品成功：【{}】",commodityName);
        map.put("status",true);
        map.put("msg","保存商品成功");
        map.put("commodityId",commodityName);
        return map;
    }
}
